package com.zez.controller;


import com.zez.entity.TbSpecification;
import com.zez.service.TbSpecificationService;
import com.zez.util.PageResult;
import com.zez.util.Re;
import com.zez.vo.TbSpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tbspecification")
@CrossOrigin            // 跨域问题
public class TbSpecificationController {
    @Autowired
    TbSpecificationService tbSpecificationService;
    //查询所有的规格列表
    @GetMapping("/list")
    public List<TbSpecification> list(){
        return tbSpecificationService.findAll();
    }
    @GetMapping("/findByPage")
    public PageResult<TbSpecification> findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        return tbSpecificationService.findByPage(page,pageSize);
    }
    //分页查找规格，其中page,pageSize为必须参数，规格对象为可选参数
    @PostMapping("/search")
    public PageResult<TbSpecification> search(@RequestParam("page") Integer page,
                                              @RequestParam("pageSize") Integer pageSize,
                                              @RequestBody(required = false) TbSpecificationVO tbSpecificationVO){
        return tbSpecificationService.search(page,pageSize,tbSpecificationVO);
    }

    // 4. 保存规格
    @PostMapping("/save")
    public Re save(@RequestBody TbSpecification tbSpecification){
        return tbSpecificationService.save(tbSpecification) ? Re.ok().message("规格添加成功！") : Re.error().message("规格添加失败！");
    }
}
